import { defineStore } from 'pinia'

export const useAppStore = defineStore('app', {
  state: () => ({
    theme: localStorage.getItem('theme') || 'light',
    locale: localStorage.getItem('locale') || 'zh_CN',
    notifications: [],
    loading: false
  }),

  actions: {
    setTheme(theme) {
      this.theme = theme
      localStorage.setItem('theme', theme)
      document.documentElement.setAttribute('data-theme', theme)
    },

    setLocale(locale) {
      this.locale = locale
      localStorage.setItem('locale', locale)
    },

    setLoading(loading) {
      this.loading = loading
    },

    addNotification(notification) {
      const id = Date.now()
      this.notifications.push({
        id,
        type: notification.type || 'info',
        message: notification.message,
        duration: notification.duration || 3000
      })

      if (notification.duration !== 0) {
        setTimeout(() => {
          this.removeNotification(id)
        }, notification.duration)
      }
    },

    removeNotification(id) {
      this.notifications = this.notifications.filter(n => n.id !== id)
    },

    success(message, duration = 3000) {
      this.addNotification({
        type: 'success',
        message,
        duration
      })
    },

    error(message, duration = 3000) {
      this.addNotification({
        type: 'error',
        message,
        duration
      })
    },

    warning(message, duration = 3000) {
      this.addNotification({
        type: 'warning',
        message,
        duration
      })
    },

    info(message, duration = 3000) {
      this.addNotification({
        type: 'info',
        message,
        duration
      })
    }
  }
}) 